using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.Services;
using JGSY.CMS.LowCode.Platform.Infrastructure.Middleware;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Extensions
{
    /// <summary>
    /// 日志服务扩展
    /// 简化日志相关服务和中间件的注册
    /// </summary>
    public static class LoggingServiceExtensions
    {
        /// <summary>
        /// 添加综合日志服务
        /// 注册异常日志、操作日志、安全日志、审计日志服务
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection AddComprehensiveLogging(this IServiceCollection services)
        {
            // 注册日志应用服务
            services.AddScoped<IExceptionLogAppService, ExceptionLogAppService>();
            services.AddScoped<IOperationLogAppService, OperationLogAppService>();
            services.AddScoped<ISecurityLogAppService, SecurityLogAppService>();
            services.AddScoped<IAuditLogAppService, AuditLogAppService>();

            return services;
        }

        /// <summary>
        /// 配置日志中间件管道
        /// 按正确顺序添加所有日志中间件
        /// </summary>
        /// <param name="app">应用程序</param>
        /// <param name="options">日志中间件配置选项</param>
        /// <returns>应用程序</returns>
        public static IApplicationBuilder UseComprehensiveLogging(this IApplicationBuilder app, LoggingMiddlewareOptions? options = null)
        {
            options ??= new LoggingMiddlewareOptions();

            // 按照正确的顺序添加中间件
            if (options.UseExceptionLogging)
                app.UseMiddleware<ExceptionHandlingMiddleware>();
            
            if (options.UseSecurityLogging)
                app.UseMiddleware<SecurityLoggingMiddleware>();
            
            if (options.UseOperationLogging)
                app.UseMiddleware<OperationLoggingMiddleware>();
            
            if (options.UseAuditLogging)
                app.UseMiddleware<AuditLoggingMiddleware>();

            return app;
        }
    }

    /// <summary>
    /// 日志中间件配置选项
    /// </summary>
    public class LoggingMiddlewareOptions
    {
        /// <summary>
        /// 启用异常日志中间件
        /// </summary>
        public bool UseExceptionLogging { get; set; } = true;

        /// <summary>
        /// 启用操作日志中间件
        /// </summary>
        public bool UseOperationLogging { get; set; } = true;

        /// <summary>
        /// 启用安全日志中间件
        /// </summary>
        public bool UseSecurityLogging { get; set; } = true;

        /// <summary>
        /// 启用审计日志中间件
        /// </summary>
        public bool UseAuditLogging { get; set; } = true;

        /// <summary>
        /// 创建默认配置
        /// </summary>
        /// <returns>默认配置</returns>
        public static LoggingMiddlewareOptions Default() => new();

        /// <summary>
        /// 创建仅异常日志的配置
        /// </summary>
        /// <returns>异常日志配置</returns>
        public static LoggingMiddlewareOptions ExceptionOnly() => new()
        {
            UseExceptionLogging = true,
            UseOperationLogging = false,
            UseSecurityLogging = false,
            UseAuditLogging = false
        };

        /// <summary>
        /// 创建安全相关日志的配置
        /// </summary>
        /// <returns>安全日志配置</returns>
        public static LoggingMiddlewareOptions SecurityFocused() => new()
        {
            UseExceptionLogging = true,
            UseOperationLogging = false,
            UseSecurityLogging = true,
            UseAuditLogging = true
        };

        /// <summary>
        /// 创建性能监控配置
        /// </summary>
        /// <returns>性能监控配置</returns>
        public static LoggingMiddlewareOptions PerformanceFocused() => new()
        {
            UseExceptionLogging = true,
            UseOperationLogging = true,
            UseSecurityLogging = false,
            UseAuditLogging = false
        };
    }
}
